package minesweeper;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Dimension;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JComponent;
import javax.swing.event.ChangeListener;


public class MineButton extends JComponent implements ButtonModel, MouseListener {
	enum State {
		DEFAULT,
		PRESSED,
		OVER,
		MINE,
		FLAG,
		QUESTION,
	};
	
	MineButton(){	
		this.setPreferredSize(new Dimension(24,24));
		//this.setBorder(BorderFactory.createLineBorder(Color.RED));
		try{
			standard = ImageIO.read(new File("button.png"));
			over = ImageIO.read(new File("button_Over.png"));
			down = ImageIO.read(new File("button_Down.png"));
			current = standard;
		} catch (IOException e){
			e.printStackTrace();
		}
		this.addMouseListener(this);
	}
	

	public boolean isEnabled() {
		return enabled;
	}
	public boolean isPressed() {
		if(state == State.PRESSED) { 
			return true; 
		} else {
			return false;
		}
	}
	
	public boolean isRollover() {
		if(state == State.QUESTION){
			return true;
		} else {
			return false;
		}
	}
	
	public boolean isSelected() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void setEnabled(boolean b) {	
		enabled = b;
	}

	public void setPressed(boolean b) {
		if(b == true){
			this.current = down;
			state = State.PRESSED;
		} else {
			this.current = standard;
			state = State.DEFAULT;
		}
		repaint();
	}

	public void setRollover(boolean b) {
		if(b == true){
			this.current = over;
			state = State.QUESTION;
		} else {
			this.current = standard;
			state = State.DEFAULT;
		}
		repaint();
	}
	
	public void setSelected(boolean b) {		
	}

	private void setButton(){
		if(state == State.DEFAULT){
			
		} else if(state == State.OVER){
			
		} else if(state == State.PRESSED){
			
		}
		
	}
	
	public void paintComponent(Graphics g){
		if(current == null) return;
		
		g.drawImage(current, 0,0,null);
		
	}
	
	public void mousePressed(MouseEvent e){
		if(e.getSource() instanceof minesweeper.MineButton){
			MineButton mb = (MineButton)e.getSource();
			if(isEnabled()){
				if(e.getButton() == MouseEvent.BUTTON1)	mb.setPressed(true);
			}
			
		}
	}
	public void mouseEntered(MouseEvent e){
		if(e.getSource() instanceof minesweeper.MineButton){
			MineButton mb = (MineButton)e.getSource();
			if(isEnabled()){
				mb.setRollover(true);
			}
			
		}
	}
	public void mouseExited(MouseEvent e){
		if(e.getSource() instanceof minesweeper.MineButton){
			MineButton mb = (MineButton)e.getSource();
			if(isEnabled()){
				mb.setRollover(false);
			}
			
		}
	}
	public void mouseReleased(MouseEvent e){
		if(e.getSource() instanceof minesweeper.MineButton){
			MineButton mb = (MineButton) e.getSource();
			if (e.getButton() == MouseEvent.BUTTON1){
				if(isEnabled()){
					mb.setPressed(false);
				}
/*				if( (e.getX() >= mb.getX() && e.getX() < mb.getX() + mb.getWidth()) && (e.getY() >= mb.getY() && e.getY() < mb.getY() + mb.getHeight())) {
					mb.setPressed(false);
				}*/
			}
		}
		
	}
	public void mouseClicked(MouseEvent e){
	}
		

	
	private State state = State.DEFAULT;
	public static final int DEFAULT_SIZE = 24;
	private boolean enabled = true;
	private Image current;
	private static Image standard; 
	private static Image over;
	private static Image down;
	
	
	
	// UNUSED FUNCTIONS
	public void addActionListener(ActionListener l) {		
	}
	public void addChangeListener(ChangeListener l) {
	}
	public void addItemListener(ItemListener l) {
		
	}
	public String getActionCommand() {
		return null;
	}
	public int getMnemonic() {
		return 0;
	}
	public boolean isArmed() {
		return false;
	}
	public Object[] getSelectedObjects() {
		return null;
	}
	public void removeActionListener(ActionListener l) {
	}
	public void removeChangeListener(ChangeListener l) {
	}
	public void removeItemListener(ItemListener l) {
	}
	public void setActionCommand(String s) {
	}
	public void setArmed(boolean b) {
	}
	public void setGroup(ButtonGroup group) {
	}
	public void setMnemonic(int key) {
	}
	//

	/* Test Main
	public static void main(String[] args){
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				JPanel panel = new JPanel();
				MineButton[] mb = new MineButton[80];
				panel.setLayout(new GridLayout(8,8));
				for(int i = 0; i < 80; i++){
					mb[i] = new MineButton();
					mb[i].setPreferredSize(new Dimension(24,24));
					panel.add(mb[i]);
					
				}
				JFrame frame = new JFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.add(panel);
				frame.pack();
				frame.setVisible(true);
			}
		});
	}
	*/
	
}

